﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	public static class DependencyContainerExtensions
	{
		//public static void Build(this IDependencyContainer source, IDependencyBuilder builder)
		//{
		//	source.Registry.Add(builder.Dependencies);
		//}

		public static bool TryResolve<T>(this IDependencyContainer source, out T instance)
		{
			object o;

			bool success = source.Resolver.TryResolve(source, new DependencyKey(typeof(T)), out o);

			if (success)
			{
				instance = (T)o;
			}
			else
			{
				instance = default(T);
			}

			return success;
		}

		public static T Resolve<T>(this IDependencyContainer source)
		{
			return (T)source.Resolver.Resolve(source, new DependencyKey(typeof(T)));
		}

		public static bool TryRelease<T>(this IDependencyContainer source, T instance)
		{
			return source.Resolver.TryRelease(source, instance);
		}

		public static void Release<T>(this IDependencyContainer source, T instance)
		{
			source.TryRelease<T>(instance);
		}

		public static void ReleaseAll(this IDependencyContainer source)
		{
			source.Lifetime.DiscardAll(source);
		}
	}
}
